﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SuperBall3D
{
    class Box : DynamicSuperBallObject
    {
        public static SoundEffect frictionsound;
        public static SoundEffectInstance frictioninstance;
        
        public Box(Game g, Vector3 pos, float xsca, float ysca, float zsca)
            : base(g, false, pos, 0.1f*xsca*ysca*zsca, .985f, .5f)
        {
            Position = pos;
            XScale = xsca*40;
            YScale = ysca*40;
            ZScale = zsca*40;
        }

        public Box(Game g, Vector3 pos)
            :this(g, pos, .1f, .1f, .1f)
        {
        }

        protected override void LoadContent()
        {
            superBallModel = Game.Content.Load<Model>("Models\\box");
            Geo = new Geometry(Geometry.GeometryType.Box, superBallModel, Position, 10,10,10);
            max_speed = 400f;
            Geo = new Geometry(Geometry.GeometryType.Box, superBallModel, Position, XScale, YScale, ZScale);
            frictionsound = Game.Content.Load<SoundEffect>("SoundEffects\\friction");
            frictioninstance = frictionsound.CreateInstance();
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            // Slows down the box
            Vector3 newVelocity = Velocity;
            newVelocity.X *= 1f - Physics.deltaT;
            newVelocity.Z *= 1f - Physics.deltaT;
            Velocity = newVelocity;

            Vector3 horiz_Velocity = Velocity;
            horiz_Velocity.Y = 0;

            // makes friction noise
            if (horiz_Velocity.Length() > 30f)
            {
                if (horiz_Velocity.Length() > max_speed) frictioninstance.Volume = 0.1f;
                else frictioninstance.Volume = 0.6f * horiz_Velocity.Length() / max_speed;
                if (frictioninstance.State == SoundState.Stopped)
                    frictioninstance.Play();
                else
                    frictioninstance.Resume();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            base.Draw(gameTime);
        }

        public override string GenerateConstructor()
        {
            return "Components.Add(new Box(this, new Vector3(" + Position.X + "f, " + Position.Y + "f, " + Position.Z + "f), " + XScale + "f, " + YScale + "f," + ZScale + "f));";
        }
       
    }

    
}
